import pygame
from constant import *

class Player(pygame.sprite.Sprite):
    '''初始化'''
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("./image/player.png")  # 加载玩家
        self.rect = self.image.get_rect()  # 设置rect区域
        self.rect.centerx = INITIAL_POSITION[0]
        self.rect.centery = INITIAL_POSITION[1]
        self.delay = MOVE_DELAY
        self.interval = MOVE_INTERVAL

    '''键盘移动'''
    def goto(self, event):
        if event.key == pygame.K_LEFT and self.rect.centerx > 0:
            self.rect.centerx = self.rect.centerx - MOVE_STEP
        if event.key == pygame.K_RIGHT and self.rect.centerx < SCREEN_WIDTH - self.rect.width:
            self.rect.centerx = self.rect.centerx + MOVE_STEP
        if event.key == pygame.K_UP and self.rect.centery > 0:
            self.rect.centery = self.rect.centery - MOVE_STEP
        if event.key == pygame.K_DOWN and self.rect.centery < SCREEN_HEIGHT - self.rect.height:
            self.rect.centery = self.rect.centery + MOVE_STEP
